<?php

namespace qrcode;

class QRimage {

    /**
     * 生成二维码
     * @param $frame
     * @param bool $filename
     * @param null $logo
     * @param int $pixelPerPoint
     * @param int $outerFrame
     * @return string|resource
     */
    public static function png($frame, $filename = false, $logo=null, $pixelPerPoint = 4, $outerFrame = 4)
    {
        $image = self::image($frame, $pixelPerPoint, $outerFrame);
        if ($filename === false) {
            $image = self::getImageContent($image);
            if (!empty($logo)){
                $imageContent = self::createLogo($image,$logo);
                $image = self::getImageContent($imageContent);
            }
            return $image;
        }else{
            if (!empty($logo)){ //先生成logo内容再生成文件
                $imageContent = self::getImageContent($image);
                $image = self::createLogo($imageContent,$logo);
            }
            ImagePng($image, $filename);
            ImageDestroy($image);
            return $filename;
        }
    }

    /**
     * 创建logo
     * @param $imageContent
     * @param $logo
     * @return false|resource
     */
    private static function createLogo($imageContent,$logo){
        $imageContent = imagecreatefromstring($imageContent);   		//目标图象连接资源。
        $logo = imagecreatefromstring(file_get_contents($logo));   	//源图象连接资源。
        $QRWidth = imagesx($imageContent);			//二维码图片宽度
        $QRHeight = imagesy($imageContent);		//二维码图片高度
        $logoWidth = imagesx($logo);		//logo图片宽度
        $logoHeight = imagesy($logo);		//logo图片高度
        $logoQrWidth = $QRWidth / 4;   	//组合之后logo的宽度(占二维码的1/5)
        $scale = $logoWidth/$logoQrWidth;   	//logo的宽度缩放比(本身宽度/组合后的宽度)
        $logoQrHeight = $logoHeight/$scale;  //组合之后logo的高度
        $fromWidth = ($QRWidth - $logoQrWidth) / 2;   //组合之后logo左上角所在坐标点
        imagecopyresampled($imageContent, $logo, $fromWidth, $fromWidth, 0, 0, $logoQrWidth,$logoQrHeight, $logoWidth, $logoHeight);
        ImageDestroy($logo);
        return $imageContent;
    }

    /**
     * 获取图片内容
     * @param $image
     * @return false|string
     */
    private static function getImageContent($image){
        ob_start();
        ImagePng($image);
        $imageContent = ob_get_contents();
        ImageDestroy($image);
        ob_end_clean();
        return $imageContent;
    }

    //----------------------------------------------------------------------
    private static function image($frame, $pixelPerPoint = 4, $outerFrame = 4)
    {
        $h = count($frame);
        $w = strlen($frame[0]);

        $imgW = $w + 2*$outerFrame;
        $imgH = $h + 2*$outerFrame;

        $base_image =ImageCreate($imgW, $imgH);

        $col[0] = ImageColorAllocate($base_image,255,255,255);
        $col[1] = ImageColorAllocate($base_image,0,0,0);

        imagefill($base_image, 0, 0, $col[0]);

        for($y=0; $y<$h; $y++) {
            for($x=0; $x<$w; $x++) {
                if ($frame[$y][$x] == '1') {
                    ImageSetPixel($base_image,$x+$outerFrame,$y+$outerFrame,$col[1]);
                }
            }
        }

        $target_image =ImageCreate($imgW * $pixelPerPoint, $imgH * $pixelPerPoint);
        ImageCopyResized($target_image, $base_image, 0, 0, 0, 0, $imgW * $pixelPerPoint, $imgH * $pixelPerPoint, $imgW, $imgH);
        ImageDestroy($base_image);

        return $target_image;
    }
}